local ghost_attach = fk.CreateSkill {
  name = "ghost_attach",
}

Fk:loadTranslationTable{
  ["ghost_attach"] = "查验",
  [":ghost_attach"] = "你的回合开始时，你可以秘密选择一名其他角色，若你为人类/忠臣，则在每轮结束时，若其为人/忠臣，则你从牌堆中随机获得一张红牌，若其为鬼/反贼，则你从牌堆中随机获得一张黑牌；"..
  "若你为鬼/反贼，则在每轮结束时，随机从牌堆中获得一张牌，若其不为鬼，则将其同化为鬼/将其身份转化为反贼，若其为鬼，则你死亡。",

  ["#ghost_attach-choose"] = "查验:秘密选择一名角色",
}

ghost_attach:addEffect(fk.TurnStart, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
      local tos = room:askToChoosePlayers(player, {
        targets = room:getOtherPlayers(player,false),
        min_num = 1,
        max_num = 1,
        prompt = "#ghost_attach-choose",
        skill_name = ghost_attach.name,
        cancelable = false,
        no_indicate = true,
      })
      if #tos > 0 then
        event:setCostData(self, tos)
        return true
      end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos
    room:addTableMark(player, "ghost_attach", to.id)
  end,
})


ghost_attach:addEffect(fk.RoundEnd, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return player:getTableMark("ghost_attach" > 0)
  end,
  on_cost = function (self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      if table.contains(player:getTableMark("ghost_attach"), p.id) then
        if player.role == "rebel" then
          room:addPlayerMark(p,"ghost_attach_change",1)
          local id = room:getCardsFromPileByRule(".", 1, "allPiles")
          if #id > 0 then
            room:obtainCard(player, id, false, fk.ReasonJustMove, player, ghost_attach.name)
          end
        elseif player.role == "loyalist" then
          if p.role == "rebel" then 
            local id = room:getCardsFromPileByRule(".|.|spade,club", 1, "allPiles")
            if #id > 0 then
              room:obtainCard(player, id, false, fk.ReasonJustMove, player, ghost_attach.name)
            end
          elseif p.role == "loyalist" then 
            local id = room:getCardsFromPileByRule(".|.|heart,diamond", 1, "allPiles")
            if #id > 0 then
              room:obtainCard(player, id, false, fk.ReasonJustMove, player, ghost_attach.name)
            end
          end
        end
      end     
    end
  end,
})

ghost_attach:addEffect(fk.RoundEnd, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return table.find(player.room.alive_players, function(p)
      return p:getMark("ghost_attach_change") > 0
    end)
  end,
  on_cost = function (self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      if p:getMark("ghost_attach_change") > 0 then
        room:setPlayerMark(p,"ghost_attach_change",0)
        p.role = "rebel"
        room:broadcastProperty(p, "role")
      end
    end
  end,
})

return ghost_attach
